package com.cman777.springc.sample.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cman777.springc.common.helper.JsonHelper;
import com.cman777.springc.sample.bean.dto.request.TestCrudRequest;
import com.cman777.springc.sample.bean.dto.request.TestExportRequest;
import com.cman777.springc.sample.bean.dto.request.TestPageRequest;
import com.cman777.springc.sample.bean.dto.request.TestRequest;
import com.cman777.springc.sample.bean.dto.response.TestExportResponse;
import com.cman777.springc.sample.bean.dto.response.TestPageResponse;
import com.cman777.springc.sample.bean.dto.response.TestResponse;
import com.cman777.springc.sample.bean.po.User;
import com.cman777.springc.sample.constant.CachePrefixConstants;
import com.cman777.springc.sample.service.UserService;
import com.cman777.springc.mybatis.util.PageUtil;
import com.cman777.springc.common.helper.RandomUtil;
import com.cman777.springc.redis.annotation.RedisCache;
import lombok.extern.log4j.Log4j2;
//import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenzhicong
 * @time 2020/3/17 1:04
 * @description
 */
@Component
@Log4j2
public class TestFacade {
    @Autowired
    private UserService userService;
    /*@Autowired
    private RestHighLevelClient restHighLevelClient;
*/
    @RedisCache(keyPrefix = CachePrefixConstants.TEST_CACHE_PREFIX, salt = "#request.question", ttl = 60)
    public TestResponse testCache(TestRequest request) {
        TestResponse response = new TestResponse();
        response.setAnswer(RandomUtil.did());
        return response;
    }


    public TestExportResponse testExport(TestExportRequest request) {
        IPage<User> userPage = new Page<>(request.getPageNum(), request.getPageSize());
        userPage = userService.page(userPage);
        return PageUtil.page2PageResponse(userPage, new TestExportResponse());
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void testCrud(TestCrudRequest request) {
        //插入
        //插入一个
        //testSaveOne();
        //插入多个
        //testSaveBatch();

        //删除
        //逻辑删除
        //根据id逻辑删除
        //removeById();


        //修改
        // 根据主键修改,null值不更新
        //testUpdateById();
        // 根据主键修改，null值更新
        //testUpdateAllColumnById();
        // 根据其他条件修改,null值不更新
        //testUpdateByOtherCondition();

        //查询
        selectTest();

    }

    private void testSaveOne() {
        User user = User.builder().age(26).email("c_man777@163.com").name("陈之聪").build();
        userService.save(user);
    }

    private void testSaveBatch() {
        User user1 = User.builder().age(26).email("c_man777@163.com").name("陈之聪").build();
        User user2 = User.builder().age(26).email("c_man777@163.com").name("陈之聪").build();
        List<User> users = Stream.of(user1, user2).collect(Collectors.toList());
        userService.saveBatch(users);
    }

    private void testUpdateById() {
        User user1 = User.builder().id(1L).age(26).name("陈之聪").build();
        userService.updateById(user1);
    }

    private void testUpdateAllColumnById() {
        User user1 = User.builder().id(1L).age(26).name("陈之聪").build();
        userService.updateAllColumnById(user1);
    }

    private void testUpdateByOtherCondition() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", "Sandy");
        User user = new User();
        user.setName("改个名字");
        userService.update(user, updateWrapper);
    }

    private void removeById() {
        userService.removeById(1L);
    }

    private void selectTest() {
        List<User> users = userService.list();
        log.info(JsonHelper.toJsonStringQuietly(users));
    }


    public TestPageResponse testPage(TestPageRequest request) {
        IPage<User> userPage = new Page<>(request.getPageNum(), request.getPageSize());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("name", request.getName());
        userPage = userService.page(userPage, userQueryWrapper);
        TestPageResponse testPageResponse = PageUtil.getInitPageFromPageNoList(userPage, new TestPageResponse());
        testPageResponse.setList(userPage.getRecords().stream().map((v) -> {
            TestPageResponse.UserVo userVo = new TestPageResponse.UserVo();
            userVo.setAge(v.getAge());
            userVo.setName(v.getName());
            userVo.setEmail(v.getEmail());
            return userVo;
        }).collect(Collectors.toList()));
        return testPageResponse;
    }

    public void testES() throws IOException {
   /*     String INDEX_TEST = "index_test"; // 索引名称
        String TYPE_TEST = "type_test"; // 索引类型
  */   /*  List<User> testsList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            User user = new User();
            user.setAge(18);
            user.setId(1L);
            user.setName("czc");
            testsList.add(user);
        }
        CreateIndexRequest request = new CreateIndexRequest(INDEX_TEST);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
*/
/*        GetIndexRequest request = new GetIndexRequest(INDEX_TEST);
         boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println("existsIndex: " + exists);*/


    }
}
